package cn.zhoutingrui.pethome.system.menu.controller;

import cn.zhoutingrui.pethome.myannotation.PreAuthorize;
import cn.zhoutingrui.pethome.system.menu.service.IMenuService;
import cn.zhoutingrui.pethome.system.menu.domain.Menu;
import cn.zhoutingrui.pethome.system.menu.query.MenuQuery;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存菜单",value = "menu:save")
    public AjaxResultUtil addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除菜单",value = "menu:delete")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询一个菜单",value = "menu:findOne")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "查询所有菜单",value = "menu:findAll")
    public List<Menu> list(){
        return menuService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "分页查询菜单",value = "menu:list")
    public AjaxResultUtil json(@RequestBody MenuQuery query)
    {
        try {
            List<Menu> list = menuService.findQuery(query);
            Long count = menuService.findByQueryCount(query);
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(list).setTotal(count);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail();
        }
    }

    @GetMapping("/first")
    @ApiOperation("获取菜单树")
    @PreAuthorize(name = "查询菜单树",value = "menu:tree")
    public AjaxResultUtil tree(){
        List<Menu> tree = menuService.tree();
        return AjaxResultUtil.success().setData(tree);
    }

    @PreAuthorize(name = "菜单测试",value = "menu:test")
    public void test(){

    }
}
